package com.hyw.encryption.service.providers.aes;

import java.util.Objects;
import java.util.Optional;

import org.springframework.lang.Nullable;

import com.hyw.encryption.keystore.dto.UserKeyStoreDTO;
import com.hyw.encryption.provider.IDecryptionProvider;
import com.hyw.encryption.service.IUserKeyStoreService;
import com.hyw.encryption.utils.AESUtils;

public class DefaultAESDecryptionProvider implements IDecryptionProvider {

  private final IUserKeyStoreService keyStoreService;

  public DefaultAESDecryptionProvider(IUserKeyStoreService keyStoreService) {
    super();
    Objects.requireNonNull(keyStoreService, "keyStoreService不能为空");
    this.keyStoreService = keyStoreService;
  }

  @Override
  public byte[] decryptByKey(@Nullable String key, byte[] content) {
    Optional<UserKeyStoreDTO> opt = keyStoreService.load(key);
    return AESUtils.aesDecryptByBytes(content, opt.get().getAk()).getBytes();
  }

  @Override
  public String getAlgorithm() {
    return "AES";
  }

}
